<template>
	<view class="box">
		<view class="title mlr-36 size-40 bold">
			{{type==1?'忘记密码':'重置密码'}}
		</view>
		<view class="mlr-36 size-26 gray-2 mt-10">
			使用手机号验证码找回密码
		</view>
		<view class="mlr-36 mt-40">
			<u-field v-model="tel" :trim="true" label-width="0" placeholder="请输入手机号"></u-field>
		</view>
		<view class="code mlr-36 flex">
			<input type="text" v-model="code" placeholder="请输入验证码"/>
			<view class="code1 white text-center" @click="getcode">
				{{vscode}}
			</view>
		</view>
		<view class="mlr-36 mt-10">
			<u-field v-model="pwd" :trim="true" :type="open?'text':'password'" label-width="0" maxlength="16" placeholder="请输入6-16位密码">
				<u-icon slot="right" @click="open=false" v-show="open" name="eye-fill" color="#999" size="36"></u-icon>
				<u-icon slot="right" @click="open=true" v-show="!open" name="eye-off" color="#999" size="36"></u-icon>
			</u-field>
		</view>
		<view class="mt-100">
			<u-button type="primary" :disabled="!tel || !code || !pwd" @click="submit">重置密码</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type:1,//1=忘记密码，2=重置密码
				tel: '',
				code:'',
				pwd: '',
				vscode:'获取验证码',
				countNum:60,
				countDownTimer:null,
				open: false,
				submitbtn: true
			}
		},
		onLoad(option) {
			this.type = option.type
		},
		methods: {
			// 获取短信验证码
			getcode(){
				var myreg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
				if(!myreg.test(this.tel)){
					uni.showToast({
						title:'请输入手机号',
						icon: 'none'
					})
					return
				}
				if(this.vscode != '获取验证码' && this.vscode != '重新发送'){
					return
				}
				this.countNum = 60
				this.countDownTimer = setInterval(()=> {
					if (this.countNum < 1) {
						clearInterval(this.countDownTimer)
						this.vscode = "重新发送";
						return;
					}
					this.countNum--;
					this.vscode = this.countNum + '秒重发';
				}, 1000)
				this.$http('/api/sms/send',{
					mobile: this.tel,
					event: 'resetpwd'
				},'POST').then(data=>{
					uni.showToast({
						title:'短信已发送，请注意查收',
						icon:'none'
					})
				})
			},
			// 重置密码
			submit(){
				if(this.submitbtn == true){
					this.submitbtn = false
					this.$http('/addons/ddrive/user/resetpwd',{
						mobile: this.tel,
						captcha: this.code,
						newpassword: this.pwd,
					},"POST").then(data=>{
						console.log(data);
						this.submitbtn = true
						uni.showToast({
							title:'重置密码成功'
						})
						setTimeout(()=>{
							uni.navigateBack({
								
							})
						},500)
					}).catch(()=>{
						this.submitbtn = true
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	/deep/.u-field{
		padding: 13px 0 !important;
	}
	/deep/.u-btn{
		height: 96rpx !important;
	}
	
	/deep/.u-primary-hover {
		background-color: $blue !important;
	}

	/deep/.u-btn--primary--disabled {
		background-color: $bg-1 !important;
	}

	.box {
		width: 100%;
		height: 100vh;
		overflow: hidden;

		.title {
			margin-top: 200rpx;
		}
		.code{
			margin-top: 10rpx;
			padding-top: 14rpx;
			padding-bottom: 15rpx;
			border-bottom: 1rpx solid #f5f6f7;
			input{
				margin-left: 8rpx;
				font-size: 26rpx;
			}
			.code1{
				width: 180rpx;
				height: 70rpx;
				font-size: 26rpx;
				line-height: 70rpx;
				background: $bg-1;
			}
		}
	}
</style>
